package com.softcits.autotask.service;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.softcits.autotask.quartz.HelloJob;
import com.softcits.autotask.quartz.SoftCitsConnectionProvider;

@Service
public class QuartzService {

    @Autowired
    private DataSource dataSource;

    @PostConstruct
    public void init() {
        SoftCitsConnectionProvider.setDataSource(dataSource);
    }

    public void resume(String jobName, String triggerName, String group) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(jobName, group);
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, group);

        // Grab the Scheduler instance from the Factory
        Scheduler s = StdSchedulerFactory.getDefaultScheduler();
        JobDetail jobDetail = s.getJobDetail(jobKey);

        Trigger trigger = s.getTrigger(triggerKey);
        //s.scheduleJob(jobDetail, trigger);

        // and start it off
        s.start();
        //scheduler.shutdown();
        System.out.println("Quartz is resuming...");
    }

    public void deleteJob(String jobName, String triggerName, String group) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey(jobName, group);
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, group);

        // Grab the Scheduler instance from the Factory
        Scheduler s = StdSchedulerFactory.getDefaultScheduler();
        JobDetail jobDetail = s.getJobDetail(jobKey);
        if (jobDetail != null) {
            s.deleteJob(jobKey);
            s.unscheduleJob(triggerKey);
        }
    }

    public void testQuartz01(String jobName, String triggerName, String group) {
        try {
            // Grab the Scheduler instance from the Factory
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            String cronExpression = "0/3 * * * * ? ";

            // define the job and tie it to our HelloJob class
            JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity(jobName, group).build();

            job.getJobDataMap().put("content", "What is your opinion? John");

            // Trigger the job to run now, and then repeat every 40 seconds
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, group)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)
                            .withMisfireHandlingInstructionDoNothing())
                    .build();

            // Tell quartz to schedule the job using our trigger
            scheduler.scheduleJob(job, trigger);

            // and start it off
            scheduler.start();
            //scheduler.shutdown();
            System.out.println("Quartz is running...");

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
    }
}
